
const { getAdmins, delAdmins,changeState, addAdmins, modifyAdmins } = require('../dao/adminsdao')


//获取管理员列表
module.exports.getAdmins = async params => {
    const { data, error } = await getAdmins(params);

    if (error) {
        return {
            message: '数据库错误',
            status: 0,
            error
        }
    } else {
        console.log(data);
        return {
            message: '获取数据成功',
            status: 1,
            data
        }
    }
}

// 通过id修改
module.exports.modifyAdmins = async params => {
    const { error, data } = await modifyAdmins(params)
    if (error) {
        return { message: '服务器错误', status: 0, error }
    } else {
        return { data, message: '成功', status: 1 }
    }

}
//delAdmins
module.exports.delAdmins = async params => {
    const data = await delAdmins(params);
    console.log(data, 222);
    if (data.nModified > 0) {
        return {
            message: '删除成功',
            status: 1,
            data
        }
    } else {
        return {
            message: '删除失败',
            status: 0
        }
    }
}
//changeState
module.exports.changeState =async params =>{
    const data =await changeState(params)
    console.log(data,12121);
}


//添加管理员
module.exports.addAdmins = async params => {
    console.log(params);
    const { data, error } = await getAdmins(params);
    console.log(data);
    if (error) {
        return {
            message: '添加失败，服务器错误',
            status: 0,
            error
        }
    } else if (data.length > 0) {
        return {
            message: '添加失败，该管理员已存在',
            status: 0,
        }
    } else {
        const data = await addAdmins(params)
        return { data, message: '添加成功', status: 1 }
    }
}

// //新增
// module.exports.addAdmins = async params => {
//       const data = await addAdmins(params);
//         if (data._id) {
//             if (params.adminImgs instanceof Array) {
//                 params.adminImgs.forEach(value =>
//                     removeFiles({
//                         formpath: './public/ptem',
//                         topath: './public/images',
//                         picname: value
//                     })
//                 )

//                 deleteFiles('./public/ptem')
//             } else {
//                 removeFiles({
//                     formpath: './public/ptem',
//                     topath: './public/images',
//                     picname: params.adminImgs
//                 })

//                 deleteFiles('./public/ptem')
//             }
//             return {
//                 message: '管理信息添加成功',
//                 status: 1
//             }
//         }
//     }
